package com.saint.lt.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.saint.lt.domain.Team;

@Repository
public class TeamDAOImpl implements TeamDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void add(Team team) {
		sessionFactory.getCurrentSession().save(team);
	}

	@Override
	public void remove(Team team) {
		if (null != team) {
			sessionFactory.getCurrentSession().delete(team);
		}

	}

	@Override
	public void remove(int id) {
		Team team = (Team) sessionFactory.getCurrentSession().load(Team.class,
				id);
		remove(team);
	}

	@Override
	public List<Team> list() {
		List<Team> teams = sessionFactory.getCurrentSession().createCriteria(Team.class).list();
		return teams;
	}

	@Override
	public Team get(int id) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(Team.class);
		cr.add(Restrictions.eq("id", id));
		Team result = (Team) cr.uniqueResult();
		return result;
	}

}
